// pages/learn/index.js
import {LearnModel} from '../../model/learn'
import { baseUrl } from "../../http/config"
import {isRegister} from '../../utils/utils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:baseUrl,
    type:1,
    // 视频
    list1: [],
    // 资料
    list2:[],
    // 试卷
    list3:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onLoad learn', options)
    isRegister()

    if(options.type == 1){
      this.typeTap(1, options.opt)
    } else if(options.type == 2){
      this.typeTap(2, options.opt)
    } else {
      this.typeTap(3, options.opt)
    }
  },
  loadVideoData(otp){
    LearnModel.courseList(otp).then((res) => {
      if(res && res.code == 200) {
        this.setData({
          list1: res.rows
        })
      }else {
        console.log(res)
      }
      //console.log('video',this.data.list1)
    }).catch(e => {
      console.log(e)
    })
  },
  loadData(otp){
    LearnModel.dataList(otp).then((res) => {
      if(res && res.code == 200) {
        this.setData({
          list2: res.rows
        })
      }else {
        console.log(res)
      }
      // console.log(this.data.list2)
    }).catch(e => {
      console.log(e)
    })
  },
  loadPaper(){
    let data = {
      "shelfStatus":1
    }
    LearnModel.paperList(data).then((res) => {
      if(res && res.code == 200) {
        this.setData({
          list3: res.data
        })
      }else {
        console.log(res)
      }
      // console.log('paperlist',this.data.list3)
    }).catch(e => {
      console.log(e)
    })
  },
  typeTap(type,otp) {
    this.setData({
      type: type
    })
    if(type == 1){
      this.loadVideoData(otp)
    } else if(type == 2){
      this.loadData(otp)
    } else {
      this.loadPaper()
    }
  }, 
  videoDetail(e) {
    isRegister()
    let id = e.currentTarget.dataset.id
    // console.log(e)
    wx.navigateTo({
      url: `./videoDetail/index?id=${id}`,
    })
  },
  dataDetail(e){
    isRegister()
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `./dataDetail/index?id=${id}`,
    })
  },
  paperDetail(e){
    isRegister()
    let id = e.currentTarget.dataset.id
    let title = e.currentTarget.dataset.title
    wx.navigateTo({
      url: `./paperDetail/index?id=${id}&title=${title}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log('onShow learn')
    // this.data.list1 = []
    // this.data.list3 = []
    // this.loadVideoData()
    // this.loadPaper()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})